package soulkun.library.helper.decoration

import android.graphics.Rect
import android.view.View
import androidx.core.view.children
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import soulkun.library.utils.toPx
import kotlin.math.max

/**
 * LinearLayoutManager布局间隔通用实现类
 * 其中填入的数字已自动转成Px，无需事先转化
 * LinearLayout：竖直布局——第一项 top space 最后一项 bottom 其他 space
 *              水平布局——第一项 left space 最后一项 right 其他 space
 */
class LinearSpaceItemDecorationHelper(
    private val topMarginDp: Int,
    private val bottomMarginDp: Int,
    private val leftMarginDp: Int,
    private val rightMarginDp: Int,
    private val orientationSpaceDp: Int
) : RecyclerView.ItemDecoration() {

    override fun getItemOffsets(
        outRect: Rect,
        view: View,
        parent: RecyclerView,
        state: RecyclerView.State
    ) {
        val layoutManager = parent.layoutManager
        if (layoutManager is LinearLayoutManager) {
            //竖直方向
            if (layoutManager.orientation == LinearLayoutManager.VERTICAL) {
                when (layoutManager.itemCount) {
                    0 -> {
                    }
                    1 -> {
                        outRect.top = topMarginDp.toPx()
                        outRect.bottom = bottomMarginDp.toPx()
                        outRect.left = leftMarginDp.toPx()
                        outRect.right = rightMarginDp.toPx()
                    }
                    else -> {
                        when {
                            parent.getChildAdapterPosition(view) == 0 -> {
                                outRect.top = topMarginDp.toPx()
                                outRect.bottom = orientationSpaceDp.toPx()
                            }
                            parent.getChildAdapterPosition(view) == layoutManager.itemCount - 1 -> {
                                outRect.bottom = bottomMarginDp.toPx()
                            }
                            else -> {
                                outRect.bottom = orientationSpaceDp.toPx()
                            }
                        }
                        outRect.left = leftMarginDp.toPx()
                        outRect.right = rightMarginDp.toPx()
                    }
                }
            }
            //水平方向
            else {
                when (layoutManager.itemCount) {
                    0 -> {
                    }
                    1 -> {
                        outRect.top = topMarginDp.toPx()
                        outRect.bottom = bottomMarginDp.toPx()
                        outRect.left = leftMarginDp.toPx()
                        outRect.right = rightMarginDp.toPx()
                    }
                    else -> {
                        when {
                            parent.getChildAdapterPosition(view) == 0 -> {
                                outRect.left = leftMarginDp.toPx()
                                outRect.right = orientationSpaceDp.toPx()
                            }
                            parent.getChildAdapterPosition(view) == layoutManager.itemCount - 1 -> {
                                outRect.right = rightMarginDp.toPx()
                            }
                            else -> {
                                outRect.right = orientationSpaceDp.toPx()
                            }
                        }
                        outRect.top = topMarginDp.toPx()
                        outRect.bottom = bottomMarginDp.toPx()
                    }
                }
            }
        }
    }

}